home *** CD-ROM | disk | FTP | other *** search
/ Amiga Magazin: Amiga-CD 1996 March / Amiga-CD 1996 #3.iso / pd-software / mui_3.1 / developer / c / examples / brush2c.c < prev    next >
C/C++ Source or Header  |  1996-01-19  |  4KB  |  134 lines

  1. /*
  2. ** This little program can be used to convert an ILBM brush
  3. ** to something that MUI's Bodychunk class can understand.
  4. **
  5. ** Perfect for about logos and stuff like that...
  6. **
  7. ** Have fun, 
  8. ** Stefan.
  9. */
  10.  
  11. #include "exec/types.h"
  12. #include "exec/memory.h"
  13. #include "libraries/dos.h"
  14. #include "libraries/iffparse.h"
  15. #include "datatypes/pictureclass.h"
  16. #include "graphics/gfx.h"
  17. #include "proto/dos.h"
  18. #include "proto/iffparse.h"
  19. #include "stdlib.h"
  20. #include "string.h"
  21. #include "stdio.h"
  22. #include "ctype.h"
  23.  
  24.  
  25. #define to32(c) (((c)<<24)|((c)<<16)|((c)<<8)|(c))
  26.  
  27.  
  28. int main(int argc,char **argv)
  29. {
  30.     char name[100],uname[100],*c;
  31.     struct IFFHandle *iff;
  32.     struct ContextNode *cn;
  33.     struct StoredProperty *sp;
  34.     struct BitMapHeader *bmhd;
  35.     UBYTE *body;
  36.     int size,i;
  37.     UBYTE *cols;
  38.  
  39.     if (argc<2 || argc>3 || argv[1][0]=='?')
  40.     {
  41.         printf("Syntax: %s ilbm-file\n",argv[0]);
  42.         exit(20);
  43.     }
  44.  
  45.     stccpy(name,argv[1],100);
  46.     if (c=strchr(name,'.')) *c=0;
  47.     strcpy(uname,name);
  48.     for (c=uname;*c;c++)
  49.         *c=toupper(*c);
  50.  
  51.     if (iff=AllocIFF())
  52.     {
  53.         if (iff->iff_Stream=Open(argv[1],MODE_OLDFILE))
  54.         {
  55.             InitIFFasDOS(iff);
  56.  
  57.             if (!OpenIFF(iff,IFFF_READ))
  58.             {
  59.                 if (!ParseIFF(iff,IFFPARSE_STEP))
  60.                 {
  61.                     if ((cn=CurrentChunk(iff)) && (cn->cn_ID==ID_FORM))
  62.                     {
  63.                         if (cn->cn_Type==ID_ILBM)
  64.                         {
  65.                             if (!PropChunk(iff,ID_ILBM,ID_BMHD) &&
  66.                                 !PropChunk(iff,ID_ILBM,ID_CMAP) &&
  67.                                 !StopChunk(iff,ID_ILBM,ID_BODY) &&
  68.                                 !StopOnExit(iff,ID_ILBM,ID_FORM) &&
  69.                                 !ParseIFF(iff,IFFPARSE_SCAN))
  70.                             {
  71.                                 if (sp=FindProp(iff,ID_ILBM,ID_CMAP))
  72.                                 {
  73.                                     cols = (UBYTE *)sp->sp_Data;
  74.  
  75.                                     printf("#ifdef USE_%s_COLORS\n",uname);
  76.                                     printf("const ULONG %s_colors[%ld] =\n{\n",name,sp->sp_Size);
  77.                                     for (i=0;i<sp->sp_Size;i+=3)
  78.                                         printf("\t0x%08lx,0x%08lx,0x%08lx,\n",to32(cols[i]),to32(cols[i+1]),to32(cols[i+2]));
  79.                                     printf("};\n");
  80.                                     printf("#endif\n\n");
  81.                                 }
  82.  
  83.                                 if (sp=FindProp(iff,ID_ILBM,ID_BMHD))
  84.                                 {
  85.                                     bmhd = (struct BitMapHeader *)sp->sp_Data;
  86.  
  87.                                     if ((bmhd->bmh_Compression==cmpNone) || (bmhd->bmh_Compression==cmpByteRun1))
  88.                                     {
  89.                                         size = CurrentChunk(iff)->cn_Size;
  90.  
  91.                                         if (body=malloc(size))
  92.                                         {
  93.                                             if (ReadChunkBytes(iff,body,size)==size)
  94.                                             {
  95.                                                 fprintf(stderr,"Width %d Height %d Depth %d - converting...\n",bmhd->bmh_Width,bmhd->bmh_Height,bmhd->bmh_Depth);
  96.  
  97.                                                 printf("#define %s_WIDTH       %3d\n",uname,bmhd->bmh_Width);
  98.                                                 printf("#define %s_HEIGHT      %3d\n",uname,bmhd->bmh_Height);
  99.                                                 printf("#define %s_DEPTH       %3d\n",uname,bmhd->bmh_Depth);
  100.                                                 printf("#define %s_COMPRESSION %3d\n",uname,bmhd->bmh_Compression);
  101.                                                 printf("#define %s_MASKING     %3d\n",uname,bmhd->bmh_Masking);
  102.                                                 printf("\n");
  103.  
  104.                                                 printf("#ifdef USE_%s_HEADER\n",uname);
  105.                                                 printf("const struct BitMapHeader %s_header =\n{ %ld,%ld,%ld,%ld,%ld,%ld,%ld,0,%ld,%ld,%ld,%ld,%ld };\n",name,bmhd->bmh_Width,bmhd->bmh_Height,bmhd->bmh_Left,bmhd->bmh_Top,bmhd->bmh_Depth,bmhd->bmh_Masking,bmhd->bmh_Compression,bmhd->bmh_Transparent,bmhd->bmh_XAspect,bmhd->bmh_YAspect,bmhd->bmh_PageWidth,bmhd->bmh_PageHeight);
  106.                                                 printf("#endif\n\n");
  107.  
  108.                                                 printf("#ifdef USE_%s_BODY\n",uname);
  109.                                                 printf("const UBYTE %s_body[%ld] = {\n",name,size);
  110.                                                 for (i=0;i<size;i++)
  111.                                                 {
  112.                                                     printf("0x%02lx,",body[i]);
  113.                                                     if (!((i+1)%15)) printf("\n");
  114.                                                 }
  115.                                                 printf(" };\n");
  116.                                                 printf("#endif\n");
  117.                                             }
  118.                                             free(body);
  119.                                         }
  120.                                     }
  121.                                 }
  122.                             }
  123.                         }
  124.                     }
  125.                 }
  126.                 CloseIFF(iff);
  127.             }
  128.             Close(iff->iff_Stream);
  129.         }
  130.         FreeIFF(iff);
  131.     }
  132.     return(0);
  133. }
  134.